package cn.gdcp.contentprovidetest;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class ContactAdapter extends ArrayAdapter<Contact> {
       private int resourceId;
    public ContactAdapter( Context context, int resource,   List<Contact> list ) {
        super(context, resource, list);
        this.resourceId = resource;
    }

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
       ViewHolder holder;
      Contact contact=getItem(position);
        if (convertView==null){
       view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
         holder=new ViewHolder();
          holder.nameText=view.findViewById(R.id.name);
          holder.numberText=view.findViewById(R.id.phone_number);
            view.setTag(holder);
        }else {
            view=convertView;
             holder= (ViewHolder)view.getTag();
            }
          holder.nameText.setText(contact.getName());
            holder.numberText.setText(contact.getNember());
        return view;
    }
    static class  ViewHolder {
        TextView nameText;
        TextView numberText;
    }
}
